
<template>
    <div class="page searchbar-result com-border-bottom" id="searchResult" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);" v-if="searchList">
          <ul class="user-list">
              <li v-for="item in items" class="mod-user com-border-bottom">
                  <a v-if="origin_page == 'xmmember'" @click="toxmUserInfo(item.user_id,item.group_id)">
                      <img v-if="item.logo.thumb_url" class="avatar" :src="item.logo.thumb_url" alt="">
                      <img v-else  class="avatar" src="../../static/imgs/avatar.png">
                      <div class="info">
                          <p class="name">{{item.user_name}}</p>
                          <p class="team">{{item.group_name}}</p>
                      </div>
                      <span @click.stop="callPhone(item.mobile)" class="bg_sprite phone"></span>
                  </a>
                  <a v-else @click="toteamUserInfo(item.user_id,item.group_id,item.user_name,item.mobile,item.role,item.role_name)">
                      <img v-if="item.logo.thumb_url" class="avatar" :src="item.logo.thumb_url" alt="">
                      <img v-else  class="avatar" src="../../static/imgs/avatar.png">
                      <div class="info">
                          <p class="name">{{item.user_name}}</p>
                          <p class="team">{{item.group_name}}</p>
                      </div>
                      <span @click.stop="callPhone(item.mobile)" class="bg_sprite phone"></span>
                  </a>
              </li>
          </ul>
    </div>
    <div v-show="searching" class="mod-empty new_mod-empty">
       <!--  <div class="icon icon_4"></div>
        <p class="tip">{{messageTitle}}</p>
        <p class="tip_2">可根据姓名、拼音、手机号进行搜索</p> -->
        <div class="search_tip">你可以搜索</div>
        <div class="searchName">
            <img src="../../static/imgs/icons/icon_8.png" class="searchName_img">
            <p class="searchName_tip">姓名</p>
        </div>
        <div class="line"></div>
        <div class="searchPhone">
            <img src="../../static/imgs/icons/icon_9.png" class="searchPhone_img">
            <p class="searchPhone_tip">手机号</p>
        </div>
    </div>
    <div v-show="noData" class="mod-empty new_mod-empty">
        <div class="icon icon_4"></div>
        <!-- <p class="tip">未搜索到<span style="color:rgb(255, 187, 0);">{{searchKey}}</span>相关人员</p> -->
        <p class="tip">什么都没有搜到耶</p>
    </div>
    <div class="page__bd" v-show="showMore">
      <div class="weui-loadmore">
          <i class="weui-loading"></i>
          <span class="weui-loadmore__tips">正在加载</span>
      </div>
    </div>
</template>
<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi'
    import Empty from 'components/Empty/Empty'
    import * as util from 'js/util.js'
    import GroupUtil from 'js/GroupUtil.js'
    import searchApi from 'clientApi/searchPage'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
     let offset = 1 // 返回记录行的偏移量(从第几条数据开始取
     let rows = 10 // 返回记录行的最大条数（取的条数）
     let canLoadMore = 0
    /**
     *  组件实例对象
     */
    export default {
        components: {
            Empty
        },
        data(){
            return{
                items: [],
                status: '',
                loading: false,
                introMsg: false,
                noData: false,
                message:'没有搜索到该成员',
                groupId: '',
                userId: '',
                teamId: '',
                projectId: '',
                showMore:false,
                searching: true,
                searchList:false,
                searchKey: '',
                origin_page: '',//页面来源 
                messageTitle: '',//提示文字
                dataUrl: '',//请求接口
                inParam: {},//入参
                department_id: '',//部门ID
                memberData: '',//操作用户
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
              this.origin_page = util.getUrddataString('originpage')
              this.teamId = util.getUrddataString('tid')
              this.projectId = util.getUrddataString('pid')
              if(this.origin_page == 'xmmember'){                
                this.groupId = util.getUrddataString('gid')
                this.messageTitle = '项目人员搜索'
                // this.inParam = {
                //   team_id: this.teamId,
                //   project_id: this.projectId,
                //   group_id: this.groupId,
                //   keyword: data.keyword,
                //   page_no: offset,
                //   page_size: rows
                // }
              }else{
                this.department_id = util.getUrddataString('department_id')
                this.messageTitle = '企业成员搜索'
                // this.inParam = {
                //   team_id: this.teamId,
                //   project_id: this.projectId,
                //   keyword: data.keyword,
                //   page_no: offset,
                //   page_size: rows
                // }
              }
              // this.introMsg = true
              const setKeywords = (data) => {
                  this.searchKey = data.keyword
                  offset = 1
                  canLoadMore = 1
                  if(!this.showMore){
                    commonPage.loadPageStatus('loading')
                  }
                  if(this.origin_page == 'xmmember'){  
                    this.dataUrl = '/group/member_search'
                    this.inParam = {
                      team_id: this.teamId,
                      project_id: this.projectId,
                      group_id: this.groupId,
                      keyword: data.keyword,
                      page_no: offset,
                      page_size: rows
                    }
                  }else{
                    this.dataUrl = '/team/member/search'
                    this.inParam = {
                      team_id: this.teamId,
                      project_id: this.projectId,
                      keyword: data.keyword,
                      page_no: offset,
                      page_size: rows,
                      department_id: this.department_id
                    }
                  }
                  util.ajax({
                      url: this.dataUrl,
                      type: 'GET',
                      data: this.inParam,
                      timeout: 10000,
                      success: (obj) => { 
                        console.log('成员信息'+JSON.stringify(obj.data))
                          if(!this.showMore){
                              commonPage.loadPageStatus('loadSuccess')
                          }else{
                              this.showMore = false
                          }
                          if(obj&&obj.errno == 0){
                              if(obj.data.list.length){
                                console.log('成员信息'+JSON.stringify(obj.data))
                                this.items = obj.data.list
                                this.memberData = obj.data
                                this.noData = false;
                                this.searching = false
                                this.searchList = true
                                offset++
                              }else{
                                  this.noData = true;
                                  this.searchList = false;
                                  this.searching = false
                                  this.items = ['']
                                  canLoadMore = 0
                              }
                          }else{
                              // widget.toast(JSON.stringify(obj))
                              widget.toast(obj.errmsg)
                          }
                      },
                      error: (xhr, status) => {
                          commonPage.loadPageStatus('loadSuccess')
                          widget.toast('服务器异常')
                      },
                      noNetwork: () => {
                          commonPage.loadPageStatus('loadSuccess')
                          widget.toast('网络连接失败，请检查网络')
                      }
                  })
              }
              window.setKeywords = setKeywords

            },
            getMoreData (){
              if(!this.showMore){
                commonPage.loadPageStatus('loading')
              }
              if(this.origin_page == 'xmmember'){  
                this.dataUrl = '/group/member_search'
                this.inParam = {
                  team_id: this.teamId,
                  project_id: this.projectId,
                  group_id: this.groupId,
                  keyword: this.searchKey,
                  page_no: offset,
                  page_size: rows
                }
              }else{
                this.dataUrl = '/team/member/search'
                this.inParam = {
                  team_id: this.teamId,
                  project_id: this.projectId,
                  keyword: this.searchKey,
                  page_no: offset,
                  page_size: rows,
                  department_id: this.department_id
                }
              }
              util.ajax({
                  url: this.dataUrl,
                  type: 'GET',
                  data: this.inParam,
                  timeout: 10000,
                  success: (obj) => { 
                      if(!this.showMore){
                          commonPage.loadPageStatus('loadSuccess')
                      }else{
                          this.showMore = false
                      }
                      if(obj&&obj.errno == 0){
                          if(obj.data.list.length){
                              // this.items = obj.data.list
                              obj.data.list.forEach((item)=>{
                                this.items.push(item)
                            })
                              this.noData = false;
                              this.searching = false
                              this.searchList = true
                              offset++
                          }else{
                            // alert("nomore")
                              // this.noData = true;
                              // this.searchList = false;
                              // this.searching = false
                              // this.items = ['']
                              canLoadMore = 0
                          }
                      }else{
                          widget.toast(obj.errmsg)
                      }
                  },
                  error: (xhr, status) => {
                      commonPage.loadPageStatus('loadSuccess')
                      widget.toast('服务器异常')
                  },
                  noNetwork: () => {
                      commonPage.loadPageStatus('loadSuccess')
                      widget.toast('网络连接失败，请检查网络')
                  }
              })
            },
            //组织架构成员
            toteamUserInfo (userId,gId,userName,userMobile,roleId,roleName){
              // alert(userId+gId+userName+userMobile+roleId+roleName)
              let str = `teamId=${this.teamId}&user_id=${userId}&department_id=${this.department_id}&name=${userName}&currRole=${this.memberData.user.role_name}&role=${roleName}&mobile=${userMobile}&currentLoginUserId=${this.memberData.user.user_id}&roleid=${roleId}`
              console.log(str)
              let encodeStr = encodeURIComponent(str)
              location.href = `dmsaas://MemberInfo?urdData=${encodeStr}`
            },
            //项目成员
            toxmUserInfo (userId,gId){
              let url = `uid=${userId}&gid=${gId}&tid=${this.teamId}&pid=${this.projectId}`
              location.href = "dmsaas://ProjectMemberDetail?urdData=" + encodeURIComponent(url)
            },
            //拨打电话
            callPhone (mobile){
              let title = '拨打电话';
              let message = mobile;
              let okBtn = '确定';
              let cancelBtn = '取消';
              let imageName = '';
              clientUi.showPhoneDialog(title, message, okBtn, cancelBtn, imageName, () => {})
            }
        },
        created(){

            this.init();
            commonPage.registerHitPageBottomListener(() => {
                if(canLoadMore==0){
                    return
                }
                // this.showMore = true
                this.getMoreData();
            }, 0)

        },
        ready() {
            //搜索结果
            // const getSearchResult = (listData, keyword) => {
            //     // alert(JSON.stringify(listData))
            //     this.items = listData

            //     // alert(JSON.stringify(listData))

            //     if (keyword){
            //       this.introMsg = false
            //       this.noData = true
            //     } else {
            //       this.introMsg = true
            //       this.noData = false
            //     }
            // }
            // window.getSearchResult = getSearchResult


        }

    }

</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/user-list.css"></style>
<style>
    #searchResult{
        top: 0px;
    }
</style>
